# -*- coding: utf8 -*-
"""
Created on Tue Oct 16 16:31:24 2012

@author: (Ago) Milan Augustin

aaaugustin@gmail.com
"""

import sys, os
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import QSettings
from ui_mainwind3d import Ui_MainWindow
import vtk

# Vlastní moduly
from osaZ import PohybZ
from slideshow import Projektor
from krajec import Krajec
from spuKrajec import SimpleView                                            

                                 

class SpustitWindow(QtGui.QMainWindow, Ui_MainWindow):

    def __init__(self, parent = None):
        
        self.fileName =os.getcwd()+"\Model_Stl\kalibr.stl"
        self.value =1       
        QtGui.QMainWindow.__init__(self) #
        
        self.view = vtk.vtkGraphLayoutView()
        self.setupUi(self)
        
         
        self.connect(self.nahoru, QtCore.SIGNAL("clicked()"), self.onNahoru)
        self.connect(self.stop, QtCore.SIGNAL("clicked()"), self.onStop)
        self.connect(self.dolu, QtCore.SIGNAL("clicked()"), self.onDolu)
        self.connect(self.tisk, QtCore.SIGNAL("clicked()"), self.onProjekce)
        self.connect(self.krokNahoru, QtCore.SIGNAL("clicked()"), self.onKrokN)
        self.connect(self.krokDolu, QtCore.SIGNAL("clicked()"), self.onKrokD)
        self.connect(self.potvrdNast, QtCore.SIGNAL("clicked()"), self.onPotvrdit)        
        self.connect(self.nula, QtCore.SIGNAL("clicked()"), self.onPolohaO)
        self.connect(self.clona, QtCore.SIGNAL("clicked()"), self.onClona)
        self.connect(self.svetloCisti, QtCore.SIGNAL("clicked()"), self.onSvetloCisti)
       #self.connect(self.resetGrav, QtCore.SIGNAL("clicked()"), self.onReset)

#        self.openFile = QtGui.QAction(QtGui.QIcon('open.png'), 'Open .Stl', self)
#        self.openFile.setShortcut('Ctrl+O')
#        self.openFile.setStatusTip('Open new File')
#        self.openFile.triggered.connect(self.open)
#        self.menubar = self.menuBar()
#        self.fileMenu = self.menubar.addMenu('&Model')
#        self.fileMenu.addAction(self.openFile)

        self.Tisk = QtGui.QAction(QtGui.QIcon('Sablony'), 'Sablony', self)
        self.Tisk.setShortcut('Ctrl+v')
        self.Tisk.setStatusTip('Vybrat Sablony')
        self.Tisk.triggered.connect(self.sablony)
        self.menubarI = self.menuBar()
        self.fileMenuI = self.menubarI.addMenu('&Tisk')
        self.fileMenuI.addAction(self.Tisk)   
        
        self.Slicer = QtGui.QAction(QtGui.QIcon('slicer'), 'Slicer', self)
        self.Slicer.setShortcut('Ctrl+s')
        self.Slicer.setStatusTip('Slicer')
        self.Slicer.triggered.connect(self.slicer)
        self.menubarII = self.menuBar()
        self.fileMenuII = self.menubarII.addMenu('&Slicer')
        self.fileMenuII.addAction(self.Slicer)        

#        self.Konec = QtGui.QAction(QtGui.QIcon('konec'), 'Konec', self)
#        self.Konec.setShortcut('Ctrl+Q')
#        self.Konec.setStatusTip('Konec')
#        self.Konec.triggered.connect(self.close)
#        self.menubarI = self.menuBar()
#        self.fileMenuI = self.menubarI.addMenu('&Konec')
#        self.fileMenuI.addAction(self.Konec)

        self.modelStl = "Model_Stl/kalibr.stl"
        self.slozkaSablony= os.getcwd()+"/Rezy/"
        
        self.osaZ = PohybZ(self.port.isChecked())
        self.osaZ.onGravos()
        #self.reset = False
        
       # Přečte ze souboru  (.ini nastavení)      
        settings = QSettings("data.ini", QSettings.IniFormat)
        sirkaT= settings.value('sirkaT', type=int)
        vyskaT= settings.value('vyskaT', type=int)
        pozadiB4 = settings.value('pozadiB4', type=float)
        pozadiB5 = settings.value('pozadiB5', type=float)
        pozadiB6 = settings.value('pozadiB6', type=float)
        modelB1 = settings.value('modelB1', type=float)
        modelB2 = settings.value('modelB2', type=float)
        modelB3 = settings.value('modelB3', type=float)
        casTisk = settings.value('casTisk', type=int)
        podlozkaPocet = settings.value('podlozkaPocet', type=int)
        podlozkaV = settings.value('podlozkaV', type=int)
      
        #nastaví hodnoty
        self.sirkaT.setProperty("value", sirkaT)
        self.vyskaT.setProperty("value", vyskaT)
        self.doubleSpinBox_4.setProperty("value", pozadiB4)
        self.doubleSpinBox_5.setProperty("value", pozadiB5)
        self.doubleSpinBox_6.setProperty("value", pozadiB6)
        self.doubleSpinBox.setProperty("value", modelB1)
        self.doubleSpinBox_2.setProperty("value", modelB2)
        self.doubleSpinBox_3.setProperty("value", modelB3)
        self.expozice.setProperty("value", casTisk)
        self.podlozkaX.setProperty("value", podlozkaPocet)
        self.podlozkaVel.setProperty("value", podlozkaV)
        
        
        self.check = True

    def testS(self):
        """
        Test metoda.
        Vypiše  text .
        """
        print "TestS OK"
        
    def keyPressEvent(self, e):
        """
        Ukonci okno aplikace
        """
        if e.key() == QtCore.Qt.Key_Escape:
            self.close()        
   
        
    def onReset(self):
        self.resetOsaZ = PohybZ(self.port.isChecked())
        self.resetOsaZ.resetGravos()
        del self.resetOsaZ
        self.onPotvrdit()
        #self.reset = True
        
   
    def onPolohaO(self):
        """
        Posle prikaz na desku Gravos (nastavi osu Z do nulove polohy)
        """
        self.osaZ.referenceZ()
        self.listWidget.addItem("    !!! PRIPRAVENO K TISKU !!!")
        
    
    def onClona(self):
        """
        Posle prikaz na Gravos (Rele(pridane) - clona obektivu)
        """
        if self.check:
            self.osaZ.onClona()
            print "A"
            self.check = False
        else:
            self.osaZ.offClona()
            print "B"
            self.check =True

        
    # pošle příkaz na Gravos  (Rele přídavné světlo - Čištění)
    # NEBO sepne rele C
    
    def onSvetloCisti(self):
        self.osaZ.onReleC()
         
    def onPotvrdit(self):
        stavP =str(self.port.isChecked())
        self.listWidget.addItem("Potvrzeno nove nastaveni.  Port -"+stavP+"-")
        del self.osaZ
        self.osaZ = PohybZ(self.port.isChecked())
        self.osaZ.onGravos()
        # zapíše nastavení - data.ini        
        settings = QSettings("data.ini", QSettings.IniFormat)
        settings.setValue('sirkaT', self.instSp.sirkaT.value())
        settings.setValue('vyskaT', self.instSp.vyskaT.value())         
        settings.setValue('podlozkaPocet', self.podlozkaX.value())
        settings.setValue('podlozkaV', self.podlozkaVel.value())         
        #form.projektor(form)

        #čas tisku ---------------------------------------        
    def casTisku(self):
        self.value =0
        self.timer = QtCore.QTimer(self)
        self.timer.timeout.connect(self.zobrazlcd)
        self.timer.start(1000)
        self.zobrazlcd() 
        
    def count(self):
        self.value = self.value+1 
        self.zobrazLcdKonec()
    def zobrazlcd(self):
        self.casTisk.display(self.value)
        casKonec =self.casKonec*60 # (s)
        self.progresCas =self.value*100/casKonec       
        self.progressBarT.setProperty("value", self.progresCas)
        self.count()
    def casTiskStop(self):
        self.timer.stop()
        
    def zobrazLcdKonec(self):
        cas = self.value
        konecC = self.casKonec*60 
        konecC = konecC - cas
        self.konecCas.display(konecC/60)
              
        #-----------------------------------------------
  

    def onNahoru(self):
        self.listWidget.addItem("Nahoru ... ")
        self.osaZ.nahoru(self.pomaluN.isChecked())
        

    def onStop(self):
        self.listWidget.addItem("Stop ! ")
        self.osaZ.stop()
        

    def onDolu(self):
        self.listWidget.addItem("Dolu ...")
        self.osaZ.dolu(self.pomaluD.isChecked()) 
        
        
    def onKrokN(self):
        if self.micA.isChecked():
            self.mic="a"
        if self.micB.isChecked():
            self.mic="b"
        if self.micC.isChecked():
            self.mic="c"   
        self.listWidget.addItem(str(self.pocetKrok.value())+ " kroku nahoru a ,b,nebo c ...")
        mic=self.mic
        self.osaZ.krokNah(mic, self.pocetKrok.value())
        
    def onKrokD(self):  
        if self.micA.isChecked():
            self.mic="a"
        if self.micB.isChecked():
            self.mic="b"
        if self.micC.isChecked():
            self.mic="c"            
        self.listWidget.addItem(str(self.pocetKrokD.value())+ "  kroku dolu a ,b,nebo c...")
        mic=self.mic
        self.osaZ.krokDol(mic, self.pocetKrokD.value())
        
                        
    def projektor(self, instSp):
        self.imageViewer = Projektor(instSp, self.osaZ, self.sirkaT.value(), self.vyskaT.value(), self.casPauza.value(), self.slozkaSablony)         
        self.instSp = instSp
    def onProjekce(self):
        del self.imageViewer
        self.imageViewer = Projektor(self.instSp, self.osaZ, self.sirkaT.value(), self.vyskaT.value(), self.casPauza.value(), self.slozkaSablony)
        self.imageViewer.casovac(self.expozice.value())
        self.imageViewer.rozliseni(self.micA.isChecked(), self.micB.isChecked(), self.micC.isChecked())
        self.imageViewer.show()
        self.osaZ.onProjektor()
        #self.imageViewer.tisk() # Spustí tisk okamžitě
 
    def open(self):
        """
        Otevre formular pro vyber .stl modelu
        """
        self.listWidget.addItem("Vyber .stl model ! ")
        self.fileName = QtGui.QFileDialog.getOpenFileName(self, "Open File",
                QtCore.QDir.currentPath())
        self.zobr3D.modelNow(self.fileName)
        popis = str(self.fileName)
        self.listWidget.addItem(popis)

    def sablony(self):
        self.listWidget.addItem("Vyber Sablony k tisku ")
        self.slozkaSablony = QtGui.QFileDialog.getExistingDirectory(self, "Open File",
                QtCore.QDir.currentPath()) 
        self.listWidget.addItem("Vybrana sablona k tisku :")        
        self.listWidget.addItem(self.slozkaSablony)  
        
    def slicer(self):
        self.window.spustit3D()
        self.window.show()
        self.window.iren.Initialize() # Need this line to actually show the render inside Qt        
        
    def slic(self, instSp):
        self.window = SimpleView(instSp)
        self.window.setInstance(self.window)


        #zobrazení řezu Záložka-----------------------------------
    def zobrazRez(self, rezX):
        
        self.grwiev = self.graphicsView
        self.scena = QtGui.QGraphicsScene()
        self.scena.addPixmap(QtGui.QPixmap(rezX))
        self.grwiev.setScene(self.scena)
        self.grwiev.show()

    
    def krajec(self, instSp):
        self.krajec = Krajec()
        self.krajec.foo = instSp # Předá odkaz na instanci "SpustitWindow"


        
    def vrstvaCis(self, index):
        self.vrstva.setProperty("intValue", index)


     #Předpokládaný čas tisku        
    def predpC(self, vrstev):
        self.casKonec =vrstev * self.expozice.value()/60  # min
        self.casKonec= self.casKonec*1.1
        self.predokladCas.setProperty("intValue", self.casKonec)
        
  
        
if __name__ == '__main__':
        
        app = QtGui.QApplication(sys.argv)
        form = SpustitWindow()
        form.show()
        form.projektor(form)
        form.slic(form)
        sys.exit(app.exec_())


                         
